

import java.io.*;
import java.net.*;
import javax.net.ssl.*;

public class Oseba{

 	String ime;
	
	public Oseba(String ime){
		this.ime=ime;
	}


}

public class ConnectionProcessor implements Runnable {

	// The Server we are running inside
	private Server server;
	// The socket connected to our client
	private Socket socket;

	private String imeStranke;
  
	// Create a new ConnectionProcessor
	public ConnectionProcessor( Server server, Socket socket ) throws IOException{
		this.server = server;
		this.socket = socket;
		imeStranke = ((SSLSocket)socket).getSession().getPeerPrincipal().getName().substring(3);
		new Thread( this ).start();
	}

	/**
	 * Background thread: read new text strings from the client,
	 * and forward them to all other clients
	 */
	@SuppressWarnings("finally")
	public void run() {
		
		try {
			PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
			BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			String inputLine;

			out.println("Pozdravljeni: " + imeStranke);
			XMLBuild xml = Server.xml;
			xml.kreirajNovoOsebo(imeStranke);
			boolean write;
			while (true){
				write = true;
					inputLine = in.readLine();
					System.out.println(inputLine);
					
					if (inputLine.split(" ")[0].equals("p") || inputLine.split(" ")[0].equals("d")){
						try{
							int idR = Integer.parseInt(inputLine.split(" ")[1]);
							int znesek = Integer.parseInt(inputLine.split(" ")[2]);
							if(inputLine.split(" ")[0].equals("d") && znesek > 0){
								try{
									if(Double.parseDouble(xml.izpisiRacunStranke(imeStranke, idR).split("\n")[2].replace("znesek = ", "")) - znesek >= 0){
										System.out.println(xml.spremeniStanjeRacuna(imeStranke, idR, -znesek));
										out.println( "Stanje se vam je zmanjsalo!");
										continue;
									}
									else
										out.println( "Na racunu nimate dovolj denarja!");
								}catch (Exception e) {
									System.out.print("Napaka v bazi!");
								}
								finally{
									continue;
								}
							}
							else{
								System.out.println(xml.spremeniStanjeRacuna(imeStranke, idR, znesek));
								out.println( "Stanje se vam je povecalo!");
							}
							
						}
						catch (Exception e) {
							e.printStackTrace();
							out.println( "Stanje se vam ni spremenilo!");
						}
					}
					else if (inputLine.equals("n")){
						System.out.println(xml.kreirajNovRacun(imeStranke, "Navadni"));
						out.println("Racun: Navadni je kreiran!");
					}
					else if (inputLine.equals("s")){
						System.out.println(xml.kreirajNovRacun(imeStranke, "Studentski"));
						out.println("Racun: Studentski je kreiran!");
					}
					else if (inputLine.equals("u")){
						System.out.println(xml.kreirajNovRacun(imeStranke, "Upokojenski"));
						out.println("Racun: Upokojenski je kreiran!");
					}
					else if (inputLine.equals("z")){
						System.out.println(xml.kreirajNovRacun(imeStranke, "Zlati"));
						out.println("Racun: Zlati je kreiran!");
					}
					else if (inputLine.equals("i")){
						write = false;
						out.println(xml.izpisiRacuneStranke(imeStranke).replace("\n", ":"));
					}
					else{
						write = false;
						out.println("Napacen ukaz!\n");
					}	
					if(write)
						xml.writeXML(new File(Server.pot));
			} 
		}
		catch( IOException ie ){
			
			try {
				socket.close();
			} 
			catch( IOException ie2 ){
				System.out.println( "Error closing socket "+socket );
			}

			server.removeConnection( this );
			System.out.println( "Closed connection from socket "+socket );
		}
	}
}
